/**
 * Description:一些注释<br/>
 * CreateTime:2019-03-27<br/>
 * Creater: yangpengfei360@126.com <br/>
 * @author yangpengfei
 */
package cn.cnsy123.bookwebsiteserver.controller.role;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.cnsy123.bookwebsiteserver.entity.SysUserRoleInfo;
import cn.cnsy123.bookwebsiteserver.param.RoleInsertParam;
import cn.cnsy123.bookwebsiteserver.param.RolePrivParam;
import cn.cnsy123.bookwebsiteserver.param.RolePrivRead;
import cn.cnsy123.bookwebsiteserver.param.RoleUpdateParam;
import cn.cnsy123.bookwebsiteserver.service.role.SysRoleInfoService;
import cn.cnsy123.bookwebsiteserver.vo.layui.CommonResult;
import cn.cnsy123.bookwebsiteserver.vo.layui.TableResult;

/**
 * Description:角色操作接口<br/>
 * CreateTime:2019-03-27<br/>
 * Creater: yangpengfei360@126.com <br/>
 * @author yangpengfei
 */
@RestController
@RequestMapping(value = "/role")
public class RoleInfoController {
    
    @Autowired
    private SysRoleInfoService sysRoleInfoService;
    
    
    @GetMapping(value = "/list")
    public TableResult<SysUserRoleInfo> findRoleInfo(
            @RequestParam(value = "page",required = true,defaultValue = "1") Integer page,
            @RequestParam(value = "limit",required = true,defaultValue = "10") Integer size){
        return sysRoleInfoService.findRoleInfo(page.intValue(),size.intValue());
    }
    
    
    @DeleteMapping(value = "/del/{roleId}")
    public CommonResult<Object> deleteRoleInfo(@PathVariable(value = "roleId") Long roleId) {
        CommonResult<Object> result = null;
        try {
            result = sysRoleInfoService.deleteRoleInfo(roleId);
        }catch(Exception e) {
            result = new CommonResult<>(1, "删除失败");
        }
        return result;
    }
    
    @PostMapping(value = "/update")
    public CommonResult<Object> updateRoleInfo(@Valid RoleUpdateParam param) {
        CommonResult<Object> result = null;
        try {
            result = sysRoleInfoService.updateRoleInfo(param);
        }catch(Exception e) {
            result = new CommonResult<>(1, "修改失败");
        }
        return result;
    }
    
    @PutMapping(value = "/insert")
    public CommonResult<Object> insertRoleInfo(@Valid RoleInsertParam param) {
        CommonResult<Object> result = null;
        try {
            result = sysRoleInfoService.insertRoleInfo(param);
        }catch(Exception e) {
            result = new CommonResult<>(1, "新增失败");
        }
        return result;
    }
    
    @PostMapping(value = "/privUpdate")
    public CommonResult<Object> updateRolePrivInfo(@Valid RolePrivParam param) {
        CommonResult<Object> result = null;
        try {
            result = sysRoleInfoService.updateRolePrivInfo(param);
        }catch(Exception e) {
            result = new CommonResult<>(1, "权限配置失败");
        }
        return result;
    }
    
    @GetMapping(value = "/privItem")
    private CommonResult<List<Map<String, String>>> findRolePrivItem(@Valid RolePrivRead param){
        CommonResult<List<Map<String, String>>> result = null;
        try {
            result = sysRoleInfoService.findRolePrivItem(param);
        }catch(Exception e) {
            result = new CommonResult<>(1, "读取权限信息失败");
        }
        return result;
    }

}
